package com.ygqh.baby.service.impl;

import com.ygqh.baby.ao.DataStatus;
import com.ygqh.baby.ao.GroupType;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.mapper.YgGroupMapper;
import com.ygqh.baby.po.YgGroup;
import com.ygqh.baby.po.YgGroupExample;
import com.ygqh.baby.po.YgGroupExample.Criteria;
import com.ygqh.baby.service.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

@Service
public class YgGroupServiceImpl implements YgGroupService {

    @Autowired
    private YgGroupMapper ygGroupMapper;
    @Autowired
    private YgProductService ygProductService;
    @Autowired
    private YgSkuService ygSkuService;
    @Autowired
    private AdminSkuService adminSkuService;
    @Autowired
    private YgGroupBuyingService ygGroupBuyingService;

    @Override
    public List<YgGroup> find() {
        YgGroupExample example = new YgGroupExample();
        example.setOrderByClause("id desc");
        example.createCriteria().andStatusNotEqualTo(DataStatus.Delete.name());
        return ygGroupMapper.selectByExample(example);
    }

    @Override
    public int findCount() {
        YgGroupExample example = new YgGroupExample();
        example.createCriteria().andStatusNotEqualTo(DataStatus.Delete.name());
        return ygGroupMapper.countByExample(example);
    }

    @Override
    public YgGroup findById(Long id) {
        return ygGroupMapper.selectByPrimaryKey(id);
    }

    @Override
    public int save(YgGroup ygGroup) {
        // 生成 分组编码

        int num = ygGroupMapper.selectMaxGroupCode();
        if (num == 0)
            num = 500001;
        else
            num++;
        ygGroup.setGroupCode(Long.valueOf(num));
        return ygGroupMapper.insertSelective(ygGroup);
    }

    @Override
    public int update(YgGroup ygGroup) {
        if (GroupType.Houtai.equals(ygGroup.getGroupType())
                && (ygGroup.getCategoryId() != null || StringUtils.isNotBlank(ygGroup.getBasicAttribute()))) {
            updateCategoryAndBasicAttributeByGroupId(ygGroup.getCategoryId(), ygGroup.getBasicAttribute(), ygGroup.getId());
        }
        return ygGroupMapper.updateByPrimaryKeySelective(ygGroup);
    }

    @Override
    public ResultSet<YgGroup> search(QueryInfo queryInfo, Long supplierId, String q, GroupType groupType, Boolean isGroup) {
        List<YgGroup> list = null;
        if (groupType.equals(GroupType.Houtai))
            list = ygGroupMapper.selectHoutaiSuper(queryInfo, supplierId, q, groupType.name(), isGroup);
        else
            list = ygGroupMapper.selectSuper(queryInfo, supplierId, q, groupType.name(), isGroup);
        int count = ygGroupMapper.countSuper(supplierId, q, groupType.name(), isGroup);
        ResultSet<YgGroup> resultSet = new ResultSet<YgGroup>(count, list);
        return resultSet;
    }

    @Override
    public int deleteSoft(Long[] ids) {
        Assert.notEmpty(ids);
        int count = 0;
        for (Long id : ids) {
            YgGroup ygGroup = this.findById(id);
            ygGroup.setStatus(DataStatus.Delete);
            count += ygGroupMapper.updateByPrimaryKeySelective(ygGroup);
        }

        return count;
    }

    @Override
    public int deleteSoft(Long id) {
        return this.deleteSoft(new Long[]{id});
    }

    @Override
    public int saveOrUpdate(YgGroup ygGroup) {
        int r = 0;
        if (ygGroup.getId() != null) {
            r = this.update(ygGroup);
        } else {
            r = this.save(ygGroup);
        }
        return r;
    }

    @Override
    public int findName(String name, Long id, GroupType type) {
        return ygGroupMapper.selectByName(name, id, type.name());
    }

    @Override
    public List<YgGroup> find(GroupType groupType, Long supplierId) {
        YgGroupExample example = new YgGroupExample();
        example.setOrderByClause("id desc");
        Criteria criteria = example.createCriteria().andStatusNotEqualTo(DataStatus.Delete.name()).andGroupTypeEqualTo(groupType.name());
        if (supplierId != null) {
            criteria.andSupplierIdEqualTo(supplierId);
        }
        return ygGroupMapper.selectByExample(example);
    }

    @Override
    public void updatePromotion(YgGroup group) {
        this.update(group);
        // 根据款式Id 更新下面的所有产品价格及分享返利率
        ygProductService.updatePromotionPrice(group.getPromotionPrice(), group.getId(), group.getUpdateBy(), group.getUpdateTime(), group.getShareRate(),
                group.getVipRate(), group.getDistributionPrice());
        // 更新款式下所有SKU 的促销价
        ygSkuService.updatePromotionPrice(group.getPromotionPrice(), group.getId(), group.getUpdateBy(), group.getUpdateTime(), group.getVipRate(),
                group.getDistributionPrice());

        ygGroupBuyingService.updateGroupBuyAmountByGroupId(group.getId(),group.getGroupAmount());

    }

    private int updateCategoryAndBasicAttributeByGroupId(Long categoryId, String basicAttribute, Long groupId) {
        if (groupId == null) {
            return 0;
        }
        List<String> productCodeList = adminSkuService.findProductCodeByGroupId(groupId);
        return ygProductService.updateCategoryAndBasicAttributeByProductCode(categoryId, basicAttribute, productCodeList);
    }

    @Override
    public List<YgGroup> findByCategoryId(Long categoryId) {
        YgGroupExample example = new YgGroupExample();
        example.createCriteria().andStatusEqualTo(DataStatus.Valid.name()).andCategoryIdEqualTo(categoryId);
        return ygGroupMapper.selectByExample(example);
    }

}
